package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class UploadSpotPeer extends BasePeer{
	public static final String tableName = "UploadSpot";
	public static final String UPLOADSPOTID = "uploadSpotId";
	public static final String FILES = "files";
	public static final String DOWNLOADS = "downloads";
	public static final String TOTALSIZE = "totalSize";
	public static final String USEDSIZE = "usedSize";
	public static final String TOTALBANDWIDTH = "totalBandwidth";
	public static final String USEDBANDWIDTH = "usedBandwidth";
	public static final String PROJECTS = "projects";
	public static final String PUBLICPOLICY = "publicPolicy";
	public static final String[] columnNames=new String[]{"uploadSpotId","files","downloads","totalSize","usedSize","totalBandwidth","usedBandwidth","projects","publicPolicy"};
	public static UploadSpot getUploadSpotByPKs(Object uploadSpotId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UploadSpotPeer.tableName,UploadSpotPeer.columnNames," TRUE  AND `uploadSpotId`=? ",new Object[]{uploadSpotId,},0, 1,conn);
			UploadSpot uploadSpot = new UploadSpot();
		fill(rs,uploadSpot);
		return uploadSpot;
	}
	public static UploadSpot getUploadSpotReferenceColumnsOnlyByPKs(Object uploadSpotId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UploadSpotPeer.tableName,new String[]{"uploadSpotId",}," TRUE AND `uploadSpotId`=?",new Object[]{uploadSpotId,}, 0,  1,conn);
			UploadSpot uploadSpot = new UploadSpot();
		fill(rs,uploadSpot);
		return uploadSpot;
	}
	public static UploadSpot getUploadSpotByUploadSpotId(long uploadSpotId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UploadSpotPeer.tableName,UploadSpotPeer.columnNames,"`uploadSpotId`=?",new Object[]{uploadSpotId},0, 1,conn);
			UploadSpot uploadSpot = new UploadSpot();
		fill(rs,uploadSpot);
		return uploadSpot;
	}
	public static void fill(ResultSet rs, UploadSpot uploadSpot) throws SQLException {
		if(rs.next()){
			try{uploadSpot.setUploadSpotId(rs.getLong("uploadSpotId"));}catch(Exception ex){}
			try{uploadSpot.setFiles(rs.getLong("files"));}catch(Exception ex){}
			try{uploadSpot.setDownloads(rs.getLong("downloads"));}catch(Exception ex){}
			try{uploadSpot.setTotalSize(rs.getLong("totalSize"));}catch(Exception ex){}
			try{uploadSpot.setUsedSize(rs.getLong("usedSize"));}catch(Exception ex){}
			try{uploadSpot.setTotalBandwidth(rs.getLong("totalBandwidth"));}catch(Exception ex){}
			try{uploadSpot.setUsedBandwidth(rs.getLong("usedBandwidth"));}catch(Exception ex){}
			try{uploadSpot.setProjects(rs.getLong("projects"));}catch(Exception ex){}
			try{uploadSpot.setPublicPolicy(rs.getString("publicPolicy"));}catch(Exception ex){}
			uploadSpot.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<UploadSpot> uploadSpotCollection) throws SQLException {
		while(rs.next()){
			UploadSpot uploadSpot=new UploadSpot();
			try{uploadSpot.setUploadSpotId(rs.getLong("uploadSpotId"));}catch(Exception ex){}
			try{uploadSpot.setFiles(rs.getLong("files"));}catch(Exception ex){}
			try{uploadSpot.setDownloads(rs.getLong("downloads"));}catch(Exception ex){}
			try{uploadSpot.setTotalSize(rs.getLong("totalSize"));}catch(Exception ex){}
			try{uploadSpot.setUsedSize(rs.getLong("usedSize"));}catch(Exception ex){}
			try{uploadSpot.setTotalBandwidth(rs.getLong("totalBandwidth"));}catch(Exception ex){}
			try{uploadSpot.setUsedBandwidth(rs.getLong("usedBandwidth"));}catch(Exception ex){}
			try{uploadSpot.setProjects(rs.getLong("projects"));}catch(Exception ex){}
			try{uploadSpot.setPublicPolicy(rs.getString("publicPolicy"));}catch(Exception ex){}
			uploadSpot.resetModified();
			uploadSpotCollection.add(uploadSpot);
		}
	}
	public static boolean update(UploadSpot uploadSpot, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < UploadSpotPeer.columnNames.length; i++) {
			if (((UploadSpotBase)uploadSpot).modified[i]) {
				columns.add(UploadSpotPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `UploadSpot` SET ");
		sql.append("WHERE TRUE AND `uploadSpotId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,uploadSpot.get(column));
		}
		pstmt.setObject(columns.size()+1+0,uploadSpot.get("uploadSpotId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(UploadSpot uploadSpot, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (uploadSpot.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `UploadSpot` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (uploadSpot.get(column) != null) {
				pstmt.setObject(i + 1 + j, uploadSpot.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<UploadSpot> getUploadSpotCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<UploadSpot> collection=new Vector<UploadSpot>();
		fill(rs,collection);
		return collection;
	}
}
